package ro.gateway.aida.db;

import java.util.Enumeration;
import java.util.Hashtable;

import ro.gateway.aida.srv.ConfigurationException;
import ro.gateway.aida.srv.IConfigurator;
import ro.gateway.aida.srv.IIDGenerator;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: StaticIdGenerator.java,v 1.1 2004/10/24 23:37:09 mihaipostelnicu Exp $
 */
public class StaticIdGenerator implements IIDGenerator
{
      private Hashtable generators;

      public StaticIdGenerator()
      {
            this.generators = new Hashtable();
      }

      public synchronized void registerGenerator( String name )
      {
            IdGen generator = new IdGen();
            generator.name = name;
            generator.initVal( 0 );
            generators.put( name, generator );
      }

      public long nextVal( String name )
      {
            IdGen generator = ( IdGen ) generators.get( name );
            if ( generator == null )
                  throw new RuntimeException( "generator not registered" );

            return generator.nextVal();
      }

      public void initGenerator( String name, long value )
      {
            IdGen generator = ( IdGen ) generators.get( name );
            if ( generator == null )
                  throw new RuntimeException( "generator not registered" );
            generator.value = value;
      }

      public void configure( IConfigurator configurator )
            throws ConfigurationException
      {
            throw new ConfigurationException( "Not implemented" );
      }

      public void reconfigure( IConfigurator configurator )
            throws ConfigurationException
      {
            throw new ConfigurationException( "Not implemented" );
      }

      public String toString()
      {
            StringBuffer sb = new StringBuffer();
            Enumeration en = generators.keys();
            while ( en.hasMoreElements() )
            {
                  String name = ( String ) en.nextElement();
                  IdGen idGen = ( IdGen ) generators.get( name );
//                  System.out.println( "currentVal(" + name + ")=" + idGen.value );
            }
            return sb.toString();
      }

      class IdGen
      {
            private long value;
            public String name;

            public synchronized long nextVal()
            {
                  return ++value;
            }

            public void initVal( long value )
            {
                  this.value = value;
            }
      }
}